

import java.awt.*;

import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;

import javax.swing.*;

import CustomEventClass.JDateEvent;
import CustomEventClass.JDateEventListener;

public class MoneyKeeperView extends JFrame{
	
	EntryForm entryForm;
	MoneyKeeperModel dbMg;
	
	JDate dtPanel;
	DataDisplayTable monthlyTable;
	DataDisplayTable dailyIncomeTable;
	DataDisplayTable dailyExpenseTable;
	private JButton modify,delete,deleteAll,excel,saveMemo, loadMemo, clearMemo;
	private ArrayList expenseList, incomeList, methodList, userList;
	private JMenuBar menuBar;
	private JMenu listMenu, helpMenu, fileMenu, screenMenu;
	private JTextArea memoArea;
	private JScrollPane scrollPane;
	private JTabbedPane tabMenu;
	private JPanel editPanel, summaryPanel,cardPanel;
	private JPanel monthlySummaryPanel, annualSummaryPanel, querySummaryPanel;
	
	
	/*
	 * Constructor for View
	 * @param MoneyKeeperModel model
	 */
	MoneyKeeperView(MoneyKeeperModel model){
		
		dbMg = model;
		this.setLayout(null);
		this.setResizable(false);
		this.getContentPane().setBackground(Color.WHITE);
		this.setTitle("MoneyKeeper Ver 1.0");
		cardPanel = new JPanel(new CardLayout());
		cardPanel.setSize(1200,725);
		
		try {			
			dbMg.openConnection();
			this.initializeEditPanel();
			this.initializeSummaryPanel();
			dbMg.closeConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}
	
	
	/*
	 * Method that initializes EditPanelGUI
	 */
	public void initializeEditPanel(){		
		
		editPanel = new JPanel();
		editPanel.setLayout(null);
		editPanel.setSize(1200,725);
		editPanel.setBackground(Color.WHITE);		
		cardPanel.add(editPanel,"EDIT");
		
		
		
		this.getContentPane().add(cardPanel);
		
		Icon boxIcon = new ImageIcon("icons\\Box_Blue.png");
		
		
		
		//Menu Bar initialization
		menuBar = new JMenuBar();
		listMenu = new JMenu("Edit");
		helpMenu = new JMenu("Help");
		fileMenu = new JMenu("File");
		screenMenu = new JMenu("Screen");
		
		//file Menu
		fileMenu.add(new JMenuItem("Exit"));
		
		//help Menu
		helpMenu.add(new JMenuItem("Help"));
		helpMenu.addSeparator();
		helpMenu.add(new JMenuItem("About"));
		
		//list Menu
		listMenu.add(new JMenuItem("Income List"));
		listMenu.add(new JMenuItem("Expense List"));
		listMenu.add(new JMenuItem("User List"));
		listMenu.add(new JMenuItem("Transaction Method List"));
		
		//screen Menu
		screenMenu.add(new JMenuItem("Update Screen"));
		screenMenu.add(new JMenuItem("Summary Screen"));
		
		//Consolidating all menu into menu bar
		menuBar.add(fileMenu);
		menuBar.add(listMenu);
		menuBar.add(screenMenu);
		menuBar.add(helpMenu);
		this.setJMenuBar(menuBar);
		
		//Setting action for File - Exit Menu
		fileMenu.getItem(0).addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ev){
				System.exit(0);
			}
		});
		//-----------------------------------
		
		//Entry form Initialization
		entryForm = new EntryForm(dbMg.loadListInArrayForm(ListModifier.INCOME_LIST),
				dbMg.loadListInArrayForm(ListModifier.EXPENSE_LIST), 
				dbMg.loadListInArrayForm(ListModifier.TRANS_METHOD_LIST), 
				dbMg.loadListInArrayForm(ListModifier.USER_LIST));
		dtPanel = new JDate();
		entryForm.setDateText(dtPanel.getDate());
		
		//Monthly Summary Table Button initialization
		modify = new JButton("MODIFY");
		delete = new JButton("DELETE SELECTED");
		deleteAll = new JButton("DELETE ALL");
		excel = new JButton("EXCEL");
		loadMemo = new JButton("Load");
		saveMemo = new JButton("Save");
		clearMemo = new JButton("Clear");
		
		//Data table initialization
		monthlyTable = new DataDisplayTable(dbMg.getMonthData(), DataDisplayTable.MONTHLY_SUMMARY);
		dailyIncomeTable = new DataDisplayTable(dbMg.getDailyIncomeData(), DataDisplayTable.DAILY_SUMMARY);
		dailyExpenseTable = new DataDisplayTable(dbMg.getDailyExpenseData(), DataDisplayTable.DAILY_SUMMARY);
		
		//Label Initialization
		JLabel dateHeader = new JLabel("DATE CALENDAR", boxIcon, JLabel.CENTER);
		JLabel memoHeader = new JLabel("MEMO", boxIcon, JLabel.LEFT);
		JLabel entryHeader = new JLabel("DATA ENTRY/MODIFICATION",boxIcon, JLabel.CENTER);
		JLabel summaryTableHeader = new JLabel("MONTHLY SUMMARY",boxIcon, JLabel.LEFT);
		JLabel incomeTableHeader = new JLabel("INCOME SUMMARY BY DAY",boxIcon, JLabel.LEFT);
		JLabel expenseTableHeader = new JLabel("EXPENSE SUMMARY BY DAY",boxIcon, JLabel.LEFT);
		
		//Memo Initialization
		memoArea = new JTextArea();
		memoArea.setLineWrap(true);
		
		loadMemo.setSize(70,22);
		loadMemo.setLocation(130,350);
		saveMemo.setSize(70,22);
		saveMemo.setLocation(210,350);
		clearMemo.setSize(70,22);
		clearMemo.setLocation(290,350);
		
		scrollPane = new JScrollPane(memoArea);
		scrollPane.setSize(350,285);
		scrollPane.setLocation(10,379);
		setMemo(dbMg.loadMemo(dbMg.getYrMonth()));
		
		
		//Button size and location initialization
		modify.setSize(100,22);
		modify.setLocation(695,349);
		delete.setSize(150,22);
		delete.setLocation(800,349);
		deleteAll.setSize(120,22);
		deleteAll.setLocation(955,349);
		excel.setSize(98,22);
		excel.setLocation(1080,349);
		
		//label size and location initialization
		dateHeader.setLocation(10,0);
		dateHeader.setSize(350,35);
		dateHeader.setFont(new Font("Calibri", Font.BOLD,20));
		memoHeader.setLocation(15,345);
		memoHeader.setSize(360,35);
		memoHeader.setFont(new Font("Calibri", Font.BOLD,20));
		entryHeader.setLocation(365, 1);
		entryHeader.setSize(350,35);
		entryHeader.setFont(new Font("Calibri", Font.BOLD,20));
		summaryTableHeader.setSize(350,35);
		summaryTableHeader.setLocation(400,345);
		summaryTableHeader.setFont(new Font("Calibri", Font.BOLD,20));
		incomeTableHeader.setSize(350,35);
		incomeTableHeader.setLocation(750,0);
		incomeTableHeader.setFont(new Font("Calibri", Font.BOLD,20));
		expenseTableHeader.setLocation(750,170);
		expenseTableHeader.setSize(350,35);
		expenseTableHeader.setFont(new Font("Calibri", Font.BOLD,20));
		
		//Other Component size and location initialization
		dtPanel.setLocation(10,35);
		dtPanel.setSize(350,300);
		monthlyTable.setLocation(380,379);
		monthlyTable.setSize(800,285);
		dailyIncomeTable.setSize(450,135);
		dailyIncomeTable.setLocation(730,35);
		dailyExpenseTable.setSize(450,135);
		dailyExpenseTable.setLocation(730,200);
		
		entryForm.setLocation(380,35);
		this.setSize(1200,725);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Adding components to content pane
		editPanel.add(entryForm);
		editPanel.add(dateHeader);
		editPanel.add(memoHeader);
		editPanel.add(entryHeader);
		editPanel.add(summaryTableHeader);
		editPanel.add(incomeTableHeader);
		editPanel.add(expenseTableHeader);
		editPanel.add(dtPanel);
		editPanel.add(monthlyTable);
		editPanel.add(dailyIncomeTable);
		editPanel.add(dailyExpenseTable);
		editPanel.add(modify);
		editPanel.add(delete);
		editPanel.add(deleteAll);
		editPanel.add(excel);
		editPanel.add(loadMemo);
		editPanel.add(saveMemo);
		editPanel.add(clearMemo);
		editPanel.add(scrollPane);	
		
	}
	
	private void initializeSummaryPanel(){
		tabMenu = new JTabbedPane();
		summaryPanel = new JPanel();
		summaryPanel.setLayout(null);
		summaryPanel.setSize(1200,725);
		summaryPanel.setBackground(Color.WHITE);
		cardPanel.add(summaryPanel, "SUMMARY");
		
		monthlySummaryPanel = new JPanel();
		monthlySummaryPanel.setLayout(null);
		monthlySummaryPanel.setSize(1200,725);
		monthlySummaryPanel.setBackground(Color.WHITE);
		
		querySummaryPanel = new JPanel();
		querySummaryPanel.setLayout(null);
		querySummaryPanel.setSize(1200,725);
		querySummaryPanel.setBackground(Color.WHITE);
		
		annualSummaryPanel = new JPanel();
		annualSummaryPanel.setLayout(null);
		annualSummaryPanel.setSize(1200,725);
		annualSummaryPanel.setBackground(Color.WHITE);
		
		tabMenu.addTab("Daily Summary", null, querySummaryPanel, "CustomQuery");
		tabMenu.addTab("Monthly Summary", null, monthlySummaryPanel, "MonthlySummary");
		tabMenu.addTab("Annual Summary", null, annualSummaryPanel, "AnnualSummary");
		tabMenu.setSize(1200,750);
		tabMenu.setLocation(0,0);
		summaryPanel.add(tabMenu);		
	}
	
	public void addJDateEventListenerInView(JDateEventListener paramListener){
		dtPanel.addJDateEventListener(paramListener);
	}
	
	public void addResetListenerInView(ActionListener paramListener){
		entryForm.addActionListenerToReset(paramListener);
	}
	public void addSubmitListenerInView(ActionListener paramListener){
		entryForm.addActionListenerToSubmit(paramListener);
	}
	
	public void addTodayListenerInView(ActionListener paramListener){
		entryForm.addActionListenerToTodayButton(paramListener);
	}
	
	public void addExcelListener(ActionListener paramListener){
		excel.addActionListener(paramListener);
	}
	public void addDeleteListener(ActionListener paramListener){
		delete.addActionListener(paramListener);
	}
	public void addModifyListener(ActionListener paramListener){
		modify.addActionListener(paramListener);
	}
	public void addDeleteAllListener(ActionListener paramListener){
		deleteAll.addActionListener(paramListener);
	}
	
	public void addSaveMemoListener(ActionListener paramListener){
		saveMemo.addActionListener(paramListener);
	}
	public void addLoadMemoListener(ActionListener paramListener){
		loadMemo.addActionListener(paramListener);
	}
	
	public void addClearMemoListener(ActionListener paramListener){
		clearMemo.addActionListener(paramListener);
	}

	public void showMessage(String message){
		JOptionPane.showMessageDialog(this, message);		
	}
	
	public void clearMemo(){
		memoArea.setText("");
	}
	
	public String getMemo(){
		return memoArea.getText();
	}
	
	public void setMemo(String content){
		memoArea.setText(content);
	}
	
	public void addEditIncomeListListener(ActionListener paramListener){
		listMenu.getItem(0).addActionListener(paramListener);
	}
	public void addEditTransMethodListListener(ActionListener paramListener){
		listMenu.getItem(3).addActionListener(paramListener);
	}
	public void addEditExpenseListListener(ActionListener paramListener){
		listMenu.getItem(1).addActionListener(paramListener);
	}
	public void addEditUserListListener(ActionListener paramListener){
		listMenu.getItem(2).addActionListener(paramListener);
	}
	
	public void addHelpListener(ActionListener paramListener){
		helpMenu.getItem(0).addActionListener(paramListener);
	}
	
	public void addAboutListener(ActionListener paramListener){
		helpMenu.getItem(2).addActionListener(paramListener);
	}
	
	public void addCallEditScreenListener(ActionListener paramListener){
		screenMenu.getItem(0).addActionListener(paramListener);
	}
	
	public void addCallSummaryScreenListener(ActionListener paramListener){
		screenMenu.getItem(1).addActionListener(paramListener);
	}
	
	public void callEditScreen(){
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
		cl.show(cardPanel, "EDIT");	
	}
	public void callSummaryScreen(){
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
		cl.show(cardPanel, "SUMMARY");	
	}
	
	
	public void updateAllLists(){		
		entryForm.setTransIncomeList(dbMg.loadListInArrayForm(ListModifier.INCOME_LIST));
		entryForm.setTransExpenseList(dbMg.loadListInArrayForm(ListModifier.EXPENSE_LIST));
		entryForm.setTransactionMethodList(dbMg.loadListInArrayForm(ListModifier.TRANS_METHOD_LIST));
		entryForm.setUserList(dbMg.loadListInArrayForm(ListModifier.USER_LIST));
		entryForm.selectExpense();		
	}
}
	




